<template>
  <div class="search-table-page">
    <Row type="flex"
         justify="space-between"
         class="row-operation">
      <i-col span="20">
      <search-form :userType="userType"
                   @search="handleSearch"></search-form>
      </i-col>
      <i-col span="2"
           class="col-right">
      <add class="spteen"
           @getList="getTableData"></add>
      <!-- <Poptip confirm
              title="确定删除？"
              @on-ok="okdelete">
        <Button icon="md-trash"
                type="error">删除</Button>
      </Poptip> -->
      </i-col>
    </Row>
    <table-page :loading="loading"
                :columns1="columns1"
                :data1="tableData"
                :noData="noData"
                :currentPage="currentPage"
                :totalPage="totalPage"
                :pageSize="pageSize"
                @multi-select="multiSelect"
                @change-page="changePage"
                @change-page-size="changPageSize">
    </table-page>
    <edit ref="edit"
          @getList="getTableData"></edit>
  </div>
</template>

<script>
import add from './add/add'
import edit from './edit/edit'
import searchForm from './search-form/search-form'
import tablePage from '_c/table-page/table-page'
import { getUserList } from '@/api/data'
import { delSystemUser } from '@/api/user'

export default {
  name: 'user-manage',
  components: {
    add,
    edit,
    searchForm,
    tablePage
  },
  data () {
    return {
      exportLoading: false,
      loading: false,
      columns1: [
        {
          title: '登录账号',
          key: 'PhoneNum'
        },
        {
          title: '账号昵称',
          key: 'NickName'
        },
        {
          title: '用户类型',
          key: 'UserType',
          render: (h, params) => {
            const row = params.row
            const text = row.UserType === 0 ? '用户' : row.UserType === 1 ? '商家' : '授权人'
            return h('span', {}, text)
          }
        },
        {
          title: '注册时间',
          key: 'CreateTime',
          width: 180
        },
        {
          title: '用户状态',
          key: 'IsDisable',
          render: (h, params) => {
            const row = params.row
            const text = row.IsDisable === 0 ? '禁用' : '启用'
            return h('span', {}, text)
          }
        },
        {
          title: '登录状态',
          key: 'IsLog',
          render: (h, params) => {
            const row = params.row
            const text = row.IsLog === 0 ? '未登录' : '已登录'
            return h('span', {}, text)
          }
        },
        {
          title: '注册来源',
          key: 'RegistSource'
        },
        // {
        //   title: '推荐人ID',
        //   key: 'RefereeID'
        // },
        {
          title: '真实姓名',
          key: 'RealName'
        },
        {
          title: '身份证号码',
          key: 'IDCardNo',
          width: 180
        },
        {
          title: '每天奖励额',
          key: 'EveryDayReward'
        },
        {
          title: '用户积分',
          key: 'Userintegral'
        },
        {
          title: '奖励额',
          key: 'RewardQuota'
        },
        {
          title: '操作',
          key: 'handle',
          width: 80,
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small'
                },
                on: {
                  click: () => {
                    this.$refs.edit.getItem(params.row.UserID)
                  }
                }
              }, '编辑')
            ])
          }
        }
      ],
      noData: '暂无数据',
      currentPage: 1,
      totalPage: 0,
      pageSize: 10,
      tableData: [],
      selectedIds: [],
      uplistsData: {
        'PageIndex': '1',
        'PageSize': '10',
        'Search': []
      },
      userType: []
    }
  },
  mounted () {
    this.init()
    this.getTableData()
  },
  methods: {
    init () {
      this.uplistsData = {
        'PageIndex': '1',
        'PageSize': '10',
        'Search': []
      }
    },
    // 获取表格数据信息
    getTableData (parameter) {
      this.loading = true
      parameter = this.uplistsData
      getUserList(parameter).then(res => {
        if (res.data.success) {
          // console.log(res.data.data.aaData)
          this.totalPage = res.data.data.TotalCount
          this.currentPage = res.data.data.CurrentPageIndex
          this.tableData = res.data.data.aaData
          this.loading = false
        } else {
          this.$Message.info(res.data.msg)
          this.loading = true
        }
      }).catch(err => {
        console.log(err)
      })
    },
    // 页码切换
    changePage (page) {
      this.currentPage = page
      this.uplistsData.PageIndex = page
      this.getTableData()
    },
    // 切换每页显示多少条数据
    changPageSize (pageSize) {
      this.init()
      this.pageSize = pageSize
      this.uplistsData.PageSize = pageSize
      this.getTableData()
    },
    handleSearch (searchContent) {
      this.uplistsData.Search = searchContent
      // console.log(this.uplistsData)
      this.getTableData(this.uplistsData)
    },
    // 多选
    multiSelect (selection) {
      this.selectedIds = selection.map(item => item.Id)
    },
    okdelete () {
      if (this.selectedIds.length === 0) {
        this.$Message.info('请选择要删除的用户！')
      } else {
        const delData = {
          SysUserId: localStorage.getItem('userId'),
          Ids: this.selectedIds
        }
        delSystemUser(delData).then(res => {
          if (res.data.success) {
            this.$Message.info(res.data.msg)
          } else {
            this.$Message.info(res.data.msg)
          }
        }).then(() => {
          this.getTableData()
        })
      }
    }
  }
}
</script>

<style lang="less" scoped>
.search-table-page {
  padding: 20px;
  min-height: 85vh;
  background-color: #fff;
  .row-operation {
    margin-bottom: 20px;
    .col-right {
      text-align: right;
      .spteen {
        margin-left: 5px;
      }
    }
  }
}
</style>
